package com.ttyec.park.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.ttyec.park.R;
import com.ttyec.park.adapter.ViewAdapter;
import com.ttyec.park.base.BaseFragment;
import com.ttyec.park.utils.NetworkUtils;

import java.util.ArrayList;

public class HomeFragment extends BaseFragment {
    private WebView webView;
    private ViewPager viewPager;
    private WebSettings setting;
    private static final String URL = "https://www.baidu.com";

    private NetworkConnectChangedReceiver networkConnectChangedReceiver;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initWebView(view);
        return view;
    }

    private void initWebView(View view) {
        webView = (WebView) view.findViewById(R.id.webview);
        viewPager = view.findViewById(R.id.viewPager);
        setting = webView.getSettings();
        setting.setJavaScriptEnabled(true);//支持Js
        setting.setPluginState(WebSettings.PluginState.ON);
        setting.setCacheMode(WebSettings.LOAD_DEFAULT);//缓存模式
        //是否支持画面缩放，默认不支持
        setting.setBuiltInZoomControls(true);
        setting.setSupportZoom(true);
        //是否显示缩放图标，默认显示
        setting.setDisplayZoomControls(false);
        //设置默认的字符编码
        setting.setDefaultTextEncodingName("utf-8");
        //设置网页内容自适应屏幕大小
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);//注意网上例程很多的是.SINGLE_COLUMN，
        //确保跳转到另一个网页时仍然在当前WebView显示
        webView.loadUrl(URL);

        initViewPager();

        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        filter.addAction("android.net.wifi.WIFI_STATE_CHANGED");
        filter.addAction("android.net.wifi.STATE_CHANGE");
        networkConnectChangedReceiver = new NetworkConnectChangedReceiver();
        if (getActivity() != null) {
            getActivity().registerReceiver(networkConnectChangedReceiver,filter);
        }
    }

    private void initViewPager() {
        viewPager.setOffscreenPageLimit(2);
        LayoutInflater lf = getLayoutInflater().from(getActivity());
        View view1 = lf.inflate(R.layout.item_image, null);
        View view2 = lf.inflate(R.layout.item_image, null);
        View view3 = lf.inflate(R.layout.item_image, null);

        ArrayList viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        ViewAdapter adapter = new ViewAdapter(viewList);
        viewPager.setAdapter(adapter);
    }

    public class NetworkConnectChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("aijie","网络发生变化："+NetworkUtils.isConnected(context));
            if (NetworkUtils.isConnected(context)) {
                webView.setVisibility(View.VISIBLE);
                viewPager.setVisibility(View.GONE);
                webView.reload();
            } else {
                webView.setVisibility(View.GONE);
                viewPager.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onDestroyView() {
        if (getActivity() != null) {
            getActivity().unregisterReceiver(networkConnectChangedReceiver);
        }
        super.onDestroyView();
    }
}
